package mindwave.theoracle;


// ThinkGear.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, October 2012


/* A JNA interface to the thinkgear.dll library (version 2.1)
   which communicates with the Neurosky MindWave as a serial COM device.
   In the documentation, it's called the ThinkGear Communications Driver (TGCD).

   The MindWave can be found at http://store.neurosky.com/products/mindwave-1

   The DLL is part of the "MindSet Development Tools" download for 
   developers, available from 
   http://store.neurosky.com/products/developer-tools-2-1
   The DLL is from the \ThinkGear Communications Driver\win32\
   subdirectory in the unzipped download.

   JNA can be downloaded from https://github.com/twall/jna

   The constants and method signatures defined here are based on the
   C information in the thinkgear.h header file in
   \ThinkGear Communications Driver\win32\
   This header file is the best source of documentation for the ThinkGear
   functions.

   thinkgear.dll already contains some JNI method signatures, but no 
   definitions for 4 "enable" functions:

     TG_EnableLowPassFilter();
     TG_EnableBlinkDetection(); 
     TG_EnableAutoRead();
     TG_EnableZoneCalculation();

   In particular, I need TG_EnableBlinkDetection() to switch on blink detection.

   Incidentally, the header file doesn't contain a C prototype for 
   TG_EnableZoneCalculation(). I discovered it only by looking at the DLL's
   exported function list using dllexp (http://www.nirsoft.net/utils/dll_export_viewer.html). The list only gives
   function names, so I've had to guess at TG_EnableZoneCalculation's arguments. 
   In any case, I've no idea what it does :).

   I've made one design change: instead of representing the EEG wave types as
   a series of constants in this class, I've moved them into their own 
   Wave enum class. This allows 
   me to iterate through the type names, which is useful when printing their
   values (see ReadMindWave.java for an example).
*/


import com.sun.jna.Library;
import com.sun.jna.Native;


public interface ThinkGear extends Library 
{
  /* max number of connections that can be requested before being
     required to free one */
  public static final int MAX_CONNECTION_HANDLES = 128;

  // baud rates
  public static final int BAUD_1200 = 1200; 
  public static final int BAUD_2400 = 2400; 
  public static final int BAUD_4800 = 4800; 
  public static final int BAUD_9600 = 9600; 
  public static final int BAUD_57600 = 57600; 
  public static final int BAUD_115200 = 115200; 
  
  // data formats
  public static final int STREAM_PACKETS = 0; 
  public static final int STREAM_5VRAW = 1; 
  public static final int STREAM_FILE_PACKETS = 2;

  // the Wave type formats are defined in a separate Wave enum class

  // ---------------------------------------------------------
 
  ThinkGear INSTANCE = (ThinkGear)
        Native.loadLibrary("ThinkGear", ThinkGear.class);
        // create a run-time link to the DLL

  // ---------------------------------------------------------
  // method prototypes (see thinkgear.h)

  public int TG_GetDriverVersion();
  public int TG_GetNewConnectionId();

  public int TG_SetStreamLog(int connID, String filename);
  public int TG_SetDataLog(int connID, String filename);

  public int TG_WriteStreamLog(int connID, int insertTimestamp, String msg);
  public int TG_WriteDataLog(int connID, int insertTimestamp, String msg);

  public int TG_Connect(int connID, String serialPortName, 
                        int serialBaudrate, int serialDataFormat);

  public int TG_ReadPackets(int connID, int numPackets);


  public double TG_GetValue(int connID, int dataType);
  public int TG_GetValueStatus(int connID, int dataType);

  public int TG_SendByte(int connID, int b);

  public int TG_SetBaudrate(int connID, int serialBaudrate);
  public int TG_SetDataFormat(int connID, int serialDataFormat);

  public void TG_Disconnect(int connID);
  public void TG_FreeConnection(int connID);


  // new methods (see thinkgear.h)

  public int TG_EnableLowPassFilter(int connID, int rawSamplingRate);

  public int TG_EnableBlinkDetection(int connID, int enable);  
     // enable uses 0 or 1 to act as a boolean

  public int TG_EnableAutoRead(int connID, int enable);

  public int TG_EnableZoneCalculation(int connID, int enable);
  // not mentioned in thinkgear.h but exported by the DLL;
  // I'm guessing at its arguments

}  // end of ThinkGear interface



